<template>
  <div>
    <el-form :model="formItem" label-width="120px" size="mini">
      <el-form-item label="资产名称">
        <el-input v-model="formItem.s_name.value" placeholder="请输入资产名称..." />
      </el-form-item>
      <el-form-item label="资产编号">
        <el-input v-model="formItem.s_code.value" placeholder="请输入资产编号..." />
      </el-form-item>
      <el-form-item label="价值">
        <el-input v-model="formItem.s_money.value" type="number" placeholder="请输入内容">
          <el-select slot="prepend" v-model="formItem.s_selectMoney" class="select_input" placeholder="请选择" style="width:90px;">
            <el-option v-for="(item) in dataCompare" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-input>
      </el-form-item>
      <el-form-item label="是否在线">
        <el-select v-model="formItem.s_running.value" filterable clearable class="select_input" placeholder="请选择是否在线">
          <el-option v-for="(item) in isOperation" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状况">
        <el-select v-model="formItem.s_statusCode.value" filterable class="select_input" clearable placeholder="请选择资产状态">
          <el-option v-for="(item) in assetStatus" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="取得方式">
        <el-select v-model="formItem.s_assetSource.value" filterable class="select_input" clearable placeholder="请选择取得方式">
          <el-option v-for="(item) in assetSource" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="原资产卡片单位">
        <el-select v-model="formItem.s_secondDept.value" filterable class="select_input" clearable placeholder="请选择原资产卡片单位">
          <el-option v-for="(item) in secondDepts" :key="item.code" :label="item.text" :value="item.code" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { isObject } from '@neu/biz-core/utils/utils'
import { clone } from '@neu/biz-core/utils/utils'
import { findByDictionaryType } from '@neu/dcam-ui/api/dictionary'
import { tree } from '@neu/dcam-ui/api/assetType'
import * as locationApi from '@neu/dcam-ui/api/location'
import * as organizations from '@neu/auth-ui/api/organization'
import { formatDate } from '@neu/biz-core/utils/utils'

// 定义一个查询表单原始项
const formItem = {
  s_name: { value: '', label: '名称', valueName: '' },
  s_code: { value: '', label: '编码', valueName: '' },
  s_locationCode: { value: null, label: '位置', valueName: '' },
  s_assetTypeCode: { value: null, label: '资产类型', valueName: '' },
  s_orgId: { value: null, label: '部门', valueName: '' },
  s_secondDept: { value: null, label: '厅局一级下属单位' },
  s_thirdDept: { value: null, label: '厅局二级下属单位' },
  s_money: { value: '', label: '金额', valueName: '' },
  s_statusCode: { value: '', label: '状态', valueName: '' },
  s_assetSource: { value: '', label: '来源', valueName: '' },
  s_running: { value: null, label: '是否在线', valueName: '' },
  /** 金额比较 */
  s_selectMoney: { value: '', label: '操作符', valueName: '' },
  /** 维保开始时间的起止范围 */
  s_maintenanceStartBegin: { value: '', label: '维保开始起', valueName: '' },
  s_maintenanceStartEnd: { value: '', label: '维保开始止', valueName: '' },
  /** 维保结束时间的起止范围 */
  s_maintenanceEndBegin: { value: '', label: '维保结束起', valueName: '' },
  s_maintenanceEndEnd: { value: '', label: '维保结束止', valueName: '' }
}

export default {
  name: 'PermissionsTableFilter',
  props: {
    filter: {
      required: true,
      type: Function
    }
  },
  data() {
    return {
      /** 资产状态 */
      assetStatus: [],
      /** 资产来源 */
      assetSource: [],
      /** 数值型比较 */
      dataCompare: [
        { label: '>', value: 'gt' }, { label: '>=', value: 'gte' }, { label: '==', value: 'eq' }, { label: '<', value: 'lt' }, { label: '<=', value: 'lte' }
      ],
      /** 时间选择器 */
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      /** 存储时间范围 */
      timeRangeMaintenanceStart: [],
      timeRangeMaintenanceEnd: [],
      formItem: {
        ...formItem
      },
      isOperation: [{
        code: 'YES',
        text: '是'
      }, {
        code: 'NO',
        text: '否'
      }],
      /** 资产位置树 */
      availableLocations: [],
      /** 资产类型树 */
      typeOptions: [],
      secondDepts: [],
      thirdDetps: [],
      /** 使用部门 */
      availableOganizations: []
    }
  },
  created() {
    tree().then(response => {
      const result = response.data
      this.typeOptions = result
    })
    locationApi.tree().then(response => {
      this.availableLocations = response.data
    })
    organizations.tree().then(response => {
      this.availableOganizations = [response.data]
    })
    findByDictionaryType('assetStatus').then(response => {
      this.assetStatus = response.data
    })
    findByDictionaryType('assetSource').then(response => {
      this.assetSource = response.data
    })
    findByDictionaryType('secondDept').then(response => {
      this.secondDepts = response.data
    })
    findByDictionaryType('thirdDept').then(response => {
      this.thirdDetps = response.data
    })
  },
  methods: {
    // 查询按钮点击事件处理
    search: function() {
      if (this.timeRangeMaintenanceStart.length !== 0) {
        this.formItem.s_maintenanceStartBegin.value = this.timeRangeMaintenanceStart[0]
        this.formItem.s_maintenanceStartBegin.valueName = formatDate(this.timeRangeMaintenanceStart[0], 'YYYY-MM-DD')
        this.formItem.s_maintenanceStartEnd.value = this.timeRangeMaintenanceStart[1]
        this.formItem.s_maintenanceStartEnd.valueName = formatDate(this.timeRangeMaintenanceStart[1], 'YYYY-MM-DD')
      }
      if (this.timeRangeMaintenanceEnd.length !== 0) {
        this.formItem.s_maintenanceEndBegin.value = this.timeRangeMaintenanceEnd[0]
        this.formItem.s_maintenanceEndBegin.valueName = formatDate(this.timeRangeMaintenanceEnd[0], 'YYYY-MM-DD')
        this.formItem.s_maintenanceEndEnd.value = this.timeRangeMaintenanceEnd[1]
        this.formItem.s_maintenanceEndEnd.valueName = formatDate(this.timeRangeMaintenanceEnd[0], 'YYYY-MM-DD')
      }
      const value = this.formItem.s_money.value
      if (this.formItem.s_money.value !== '' && this.formItem.s_selectMoney !== '') {
        this.formItem.s_money.value = this.formItem.s_money.value + '|' + this.formItem.s_selectMoney
      }
      this.$store.dispatch('dcam/setAssetSearchObj', this.delete())

      const source = this.assetSource.filter(t => {
        return t.code === this.formItem.s_assetSource.value
      })
      this.formItem.s_assetSource.valueName = source.length === 1 ? source[0].text : this.formItem.s_assetSource.value

      const status = this.assetStatus.filter(t => {
        return t.code === this.formItem.s_statusCode.value
      })
      this.formItem.s_statusCode.valueName = status.length === 1 ? status[0].text : this.formItem.s_statusCode.value

      const locations = this.availableLocations.filter(t => {
        return t.code === this.formItem.s_locationCode.value
      })
      this.formItem.s_locationCode.valueName = locations.length === 1 ? locations[0].title : this.formItem.s_locationCode.value

      const types = this.typeOptions.filter(t => {
        return t.code === this.formItem.s_assetTypeCode.value
      })
      this.formItem.s_assetTypeCode.valueName = types.length === 1 ? types[0].title : this.formItem.s_assetTypeCode.value

      const second = this.secondDepts.filter(t => {
        return t.code === this.formItem.s_secondDept.value
      })
      this.formItem.s_secondDept.valueName = second.length === 1 ? second[0].text : this.formItem.s_secondDept.value

      const third = this.thirdDetps.filter(t => {
        return t.code === this.formItem.s_thirdDept.value
      })
      this.formItem.s_thirdDept.valueName = third.length === 1 ? third[0].text : this.formItem.s_thirdDept.value
      if (this.formItem.s_running && this.formItem.s_running.value) {
        if (this.formItem.s_running.value === 'YES') {
          this.formItem.s_running.valueName = '是'
        } else {
          this.formItem.s_running.valueName = '否'
        }
      }

      this.filter(this.formItem)
      // 回显
      this.formItem.s_money.value = value
    },
    delete() {
      let resultObj = {}
      let complexSearch = false
      const searchObj = Object.assign({}, this.formItem)
      if (Object.keys(searchObj).some(k => isObject(searchObj[k]))) {
        complexSearch = true
      }
      // 清理无效查询项
      for (const k in searchObj) {
        if (complexSearch) {
          if (!searchObj[k].value) delete searchObj[k]
        } else {
          if (!searchObj[k]) delete searchObj[k]
        }
      }

      let _searchObj = {}
      if (complexSearch) {
        for (const k in searchObj) {
          _searchObj[k] = searchObj[k].value
        }
        // 记录筛选项
        // resultObj = Object.keys(searchObj).map(key => { return { k: key, v: searchObj[key] } })
      } else {
        _searchObj = Object.assign({}, searchObj)
      }
      resultObj = _searchObj
      return resultObj
    },
    // 重置按钮点击事件处理
    reset: function() {
      for (var t in formItem) {
        formItem[t].value = null
        formItem[t].valueName = null
      }
      formItem.s_money.value = ''
      formItem.s_money.valueName = ''
      formItem.s_selectMoney.value = ''
      formItem.s_selectMoney.valueName = ''
      formItem.s_maintenanceStartBegi = { value: '', label: '维保开始起', valueName: '' }
      formItem.s_maintenanceStartEnd = { value: '', label: '维保开始止', valueName: '' }
      formItem.s_maintenanceEndBegin = { value: '', label: '维保结束起', valueName: '' }
      formItem.s_maintenanceEndEnd = { value: '', label: '维保结束止', valueName: '' }
      this.timeRangeMaintenanceStart = []
      this.timeRangeMaintenanceEnd = []
      this.formItem = clone(formItem)
      this.search()
    },
    onChange: function(val) {
      this.$parent.onResize()
    },
    onFilterItemClose: function(item) {
      if (item.k === 's_maintenanceStartBegin' || item.k === 's_maintenanceStartEnd') {
        this.formItem['s_maintenanceStartBegin'] = { value: '', label: '维保开始起', valueName: '' }
        this.formItem['s_maintenanceStartEnd'] = { value: '', label: '维保开始止', valueName: '' }
      } else if (item.k === 's_maintenanceEndBegin' || item.k === 's_maintenanceEndEnd') {
        this.formItem['s_maintenanceEndBegin'] = { value: '', label: '维保结束起', valueName: '' }
        this.formItem['s_maintenanceEndEnd'] = { value: '', label: '维保结束止', valueName: '' }
      } else {
        formItem[item.k].value = ''
        formItem[item.k].valueName = ''
        this.formItem[item.k] = clone(formItem[item.k])
      }
      this.filter(this.formItem)
    }
  }
}
</script>
<style scoped>
</style>
